//
//  FZUserAccountModel.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/22/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZUserAccountModel : NSObject, NSCoding{
    //MARK: 获取access_token 得到的数据
    /// access_token:用户授权的唯一票据，用于调用微博的开放接口，同时也是第三方应用验证微博用户登录的唯一票据
    var access_token: String?
    /// access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0{
        didSet{
//            let timeZone = NSTimeZone.systemTimeZone()
//            let interval = NSTimeInterval(timeZone.secondsFromGMTForDate(NSDate()))
            expires_date = CURRENTDATE.dateByAddingTimeInterval(expires_in)
        }
    }
    /// access_token的生命周期，单位是秒，已废弃
    var remind_in: String?
    /// 授权用户的UID
    var uid: String?
    /// 计算授权的过期日期: 计算属性必须约束类型和Var
    var expires_date: NSDate?
    
    //MARK: 用户信息
    ///screen_name用户昵称
    var screen_name: String?
    ///用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //MARK: KVC构造函数
    init(dic: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    //重写，不写参数，只是为了防止服务器更改返回参数，而我这里没有写导致的错误
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // 重写父类已有的属性description
    override var description: String{
        let keys = ["access_token", "expires_date", "uid", "screen_name" ,"avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
//MARK:==========================归档接档==========================
    
    //归档
    func encodeWithCoder(aCoder: NSCoder){
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(remind_in, forKey: "remind_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    
}

